package com.ruoyi.supplier.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.supplier.domain.GysGr;
import com.ruoyi.supplier.domain.GysQy;
import com.ruoyi.supplier.domain.GysQyAll;
import com.ruoyi.supplier.service.GysGrService;
import com.ruoyi.supplier.service.GysQyService;
import com.ruoyi.supplier.utils.BeanCopyUtils;
import com.sun.xml.bind.v2.TODO;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.GysPublic;
import com.ruoyi.supplier.service.IGysPublicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商列表Controller
 * 
 * @author itchen
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/supplier/public")
public class GysPublicController extends BaseController
{
    @Autowired
    private IGysPublicService gysPublicService;
    @Autowired
    private GysQyService gysQyService;
    @Autowired
    private GysGrService gysGrService;
    private static int sequence = 10001; // 初始化序列号
    /**
     * 查询供应商列表列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:public:list')")
    @GetMapping("/list")
    public TableDataInfo list(GysPublic gysPublic)
    {
        startPage();
        List<GysPublic> list = gysPublicService.selectGysPublicList(gysPublic);
        return getDataTable(list);
    }



    /**
     * 导出供应商列表列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:public:export')")
    @Log(title = "供应商列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GysPublic gysPublic)
    {
        List<GysPublic> list = gysPublicService.selectGysPublicList(gysPublic);
        ExcelUtil<GysPublic> util = new ExcelUtil<GysPublic>(GysPublic.class);
        util.exportExcel(response, list, "供应商列表数据");
    }

    /**
     * 获取供应商列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:public:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gysPublicService.selectGysPublicById(id));
    }

    /**
     * 新增供应商列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:public:add')")
    @Log(title = "供应商列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GysPublic gysPublic)
    {
        return toAjax(gysPublicService.insertGysPublic(gysPublic));
    }

    /**
     * 录入供应商列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('supplier:public:add')")
    @Log(title = "供应商列表", businessType = BusinessType.INSERT)
    @PostMapping("/addQyAll")
    public AjaxResult addQyAll(@RequestBody GysQyAll gysQyAll)
    {
        //判断供应商是否已存在
        String supplierName = gysQyAll.getSupplierName();
        LambdaQueryWrapper<GysPublic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GysPublic::getSupplierName,supplierName).eq(GysPublic::getDelFlag,"0");
        if (gysPublicService.getOne(wrapper)!=null){
            return AjaxResult.error("供应商名称已注册，请勿重复注册！");
        }

        //判断供应商信息类型
        if (gysQyAll.getType().equals("个人")){
            String phoneCode = gysQyAll.getPhoneCode();
            String phone = gysQyAll.getPhone();
            //TODO 待开发 传入方法(校验手机号方法)
            if (StringUtils.isNotEmpty(phoneCode) && StringUtils.isNotEmpty(phone)){
                if (phoneCode.equals("123456")){
                    return AjaxResult.error("验证码错误");
                }
                //生成供应商编码
                String code = generateSupplierCode();
                GysPublic gysPublic = BeanCopyUtils.copyBean(gysQyAll, GysPublic.class);
                gysPublic.setSupplierCode(code);
                //设置供应商状态
                gysPublic.setAdmissionStatus("潜在供应商");
                gysPublic.setStatus("待审核");
                gysPublicService.save(gysPublic);
                Long id = gysPublic.getId();
                GysGr gysGr = BeanCopyUtils.copyBean(gysQyAll, GysGr.class);
                gysGr.setPublicId(id);
                return toAjax(gysGrService.save(gysGr));
            }
            else {
                return AjaxResult.error("手机号，验证码不能为空");
            }
        }
        String code = generateSupplierCode();
        GysPublic gysPublic = BeanCopyUtils.copyBean(gysQyAll, GysPublic.class);
        gysPublic.setSupplierCode(code);
        gysPublicService.save(gysPublic);
        Long id = gysPublic.getId();
        GysQy gysQy = BeanCopyUtils.copyBean(gysQyAll, GysQy.class);
        gysQy.setPublicId(id);
        return toAjax(gysQyService.save(gysQy));
    }

    public synchronized static String generateSupplierCode() {
        // 生成编码
        String code = "S" + String.format("%05d", sequence++);
        // 为了演示，这里简单打印生成的代码，实际应用中根据需要使用
        System.out.println("Generated Supplier Code: " + code);

        return code;
    }

    /**
     * 修改供应商列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:public:edit')")
    @Log(title = "供应商列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GysPublic gysPublic)
    {
        return toAjax(gysPublicService.updateGysPublic(gysPublic));
    }

    /**
     * 删除供应商列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:public:remove')")
    @Log(title = "供应商列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gysPublicService.deleteGysPublicByIds(ids));
    }
}
